const Controller = require('egg').Controller;
// 定义删除接口的请求参数规则

class CdnController extends Controller {

    async index() {
        const { ctx } = this;
        const type = parseInt(ctx.request.query.type);
        const env = parseInt(ctx.request.query.env);
        const data = type ? { type } : {};
        if (env) {
            data.env = env;
        }
        try {
            const cdn = await ctx.service.cdn.findCdns(data);
            ctx.body = {
                code: 200,
                data: cdn,
                success: true,
                msg: ``
            };
        } catch (e) {
            throw e
        }
    }

    async create() {
        const { ctx } = this
        const data = ctx.request.body;
        try {
            const newCdn = await ctx.service.cdn.createCdn(data);
            ctx.body = {
                code: 200,
                data: newCdn,
                success: true,
                msg: ``
            };
        } catch (e) {
            throw e
        }
    }

    async destroy() {
        const ctx = this.ctx;
        const result = await ctx.service.cdn.deleteCdn(ctx.params.id);
        if (result.ok) {
            ctx.body = {
                code: 200,
                data: {},
                success: true,
                msg: `删除成功`
            }
        }
    }

    async update() {
        const ctx = this.ctx;
        const { id } = ctx.params;
        const {
            name,
            tenantId,
            version,
            homePage,
            authPage,
            resource,
            rule,
            type,
            env
        } = ctx.request.body;
        const result = await this.ctx.service.cdn.updateCdn(id, {
            name,
            tenantId,
            version,
            homePage,
            authPage,
            resource,
            rule,
            type,
            env
        });
        if (result) {
            ctx.body = {
                code: 200,
                data: result,
                success: true,
                msg: ``
            }
        }
    }

    async getVersion() {
        const ctx = this.ctx;
        const result = await ctx.service.cdn.downloadHtml(ctx.params.id);
        if (result) {
            ctx.body = {
                code: 200,
                data: result,
                success: true,
                msg: ``
            }
        }
    }

    async publishCdn() {
        const ctx = this.ctx;
        const result = await ctx.service.cdn.publishCdn(ctx.params.id);
        if (result) {
            ctx.body = {
                code: 200,
                data: result,
                success: true,
                msg: ``
            }
        }
    }

    async invalidCdn() {
        const ctx = this.ctx;
        const result = await ctx.service.cdn.invalidCdn(ctx.params.id);
        if (result) {
            ctx.body = {
                code: 200,
                data: result,
                success: true,
                msg: ``
            }
        }
    }

    async validCdn() {
        const ctx = this.ctx;
        const result = await ctx.service.cdn.validCdn(ctx.params.id);
        if (result) {
            ctx.body = {
                code: 200,
                data: result,
                success: true,
                msg: ``
            }
        }
    }
}
module.exports = CdnController;